﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Jayden.Dll.CodeGen
{
	public class CodeBuilder : IDisposable
	{
		private StringBuilder m_Builder = new StringBuilder();
		private int m_IndentCount = 0;
		private string m_IndentString = "\t";

		public CodeBuilder AppendLine()
		{
			m_Builder.AppendLine();
			return this;
		}
		public CodeBuilder AppendLine(string format, params object[] args)
		{
			for (int c = 0; c < m_IndentCount; c++)
				m_Builder.Append(m_IndentString);
			m_Builder.AppendLine(string.Format(format, args));
			return this;
		}

		public CodeBuilder OpenAndIncrementIndent(string openSymbol)
		{
			AppendLine(openSymbol);
			m_IndentCount++;
			return this;
		}
		public CodeBuilder DecrementIndentAndClose(string closeSymbol)
		{
			m_IndentCount--;
			AppendLine(closeSymbol);
			return this;
		}

		public CodeBuilder OpenParenthesis() { return OpenAndIncrementIndent("{{"); }
		public CodeBuilder CloseParenthesis() { return DecrementIndentAndClose("}}"); }

		public override string ToString() { return m_Builder.ToString(); }

		public void Dispose() { m_Builder = null; }
	}
}
